/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-15 23:36:58
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-09-15 23:43:14
 * @LastEditors: judu233
 */
export const memoize = (fn, keyFn?) => {
    if (!keyFn) {
        keyFn = function () {
            if (arguments.length === 1) {
                return arguments[0];
            } else if (arguments.length === 0) {
                return 'undefined';
            }

            let args = [];

            for (let i = 0; i < arguments.length; i++) {
                args.push(arguments[i]);
            }

            return args.join('$');
        };
    }

    let memoizedFn = function (v1,v2) {
        let self = this;
        let args = arguments;
        let ret;
        let k = keyFn.apply(self, args);
        let cache = memoizedFn[`cache`];

        if (!(ret = cache[k])) {
            ret = cache[k] = fn.apply(self, args);
        }

        return ret;
    };

    memoizedFn[`cache`] = {};

    return memoizedFn;
};
